﻿using System;
using System.ComponentModel;

namespace EasyLib.Utils
{
    public class TypeConvertUtils
    {
        /// <summary>
        /// 转换到指定类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <returns></returns>
        public static T ConvertType<T>(object o)
        {
            if (o == null) return default(T);
            if (o is T)
            {
                return (T)o;
            }
            Type dest = typeof(T);
            TypeConverter tc = TypeDescriptor.GetConverter(dest);
            return (T)tc.ConvertFrom(o);
        } 
    }
}
